define(['util', 'ajax'], function (util, ajax) {
    "use strict";


    var exports = {},
		ViewModel;

    ViewModel = function (data, inPicker) {
        var self = this;

        self.accountTypes = ko.observableArray(data.accountTypes);
        self.industries = ko.observableArray(data.industries);

        self.name = ko.observable();
        self.type = ko.observable();
        self.accountNumber = ko.observable();
        self.industry = ko.observable();
        self.billingStreet = ko.observable();
        self.billingCity = ko.observable();
        self.billingState = ko.observable();
        self.billingPostalCode = ko.observable();
        self.billingCountry = ko.observable();
        self.billingLatitude = ko.observable();
        self.billingLongitude = ko.observable();
        self.shippingStreet = ko.observable();
        self.shippingCity = ko.observable();
        self.shippingState = ko.observable();
        self.shippingPostalCode = ko.observable();
        self.shippingCountry = ko.observable();
        self.shippingLatitude = ko.observable();
        self.shippingLongitude = ko.observable();
        self.phone = ko.observable();
        self.mobile = ko.observable();
        self.fax = ko.observable();
        self.website = ko.observable();
        self.email = ko.observable();
        self.description = ko.observable();
        self.imageData = ko.observable();

        self.tempImage = ko.observable();

        self.tempImage.subscribe(function (newValue) {
            var canvas = document.getElementById("imgCanvas");
            util.image.drawImage(canvas, newValue);
            self.imageData(canvas.toDataURL());
        });

        self.tempImage(util.image.checkOrDefault(data.imageData));

        self.selectPhoto = function () {
            $("#inputFile").click();
        };

        self.capturePhoto = function () {
            $("#inputFile").click();
        };

        $.subscribe('filesDropped', function (files) {

            if (files.length == 0) return;
            var fileToLoad = files[0];
            var fileReader = new FileReader();
            fileReader.onload = function (fileLoadedEvent) {
                self.tempImage(fileLoadedEvent.target.result);
            };
            fileReader.readAsDataURL(fileToLoad);
        });


        self.save = function () {
            var type = _.find(self.accountTypes(), function (val) {
                return val.name == self.type();
            });

            var industry = _.find(self.industries(), function (val) {
                return val.name == self.industry();
            });

            var form = {
                Name: self.name(),
                AccountTypeValue: type == undefined ? '' : type.id,
                AccountNumber: self.accountNumber(),
                IndustryValue: industry == undefined ? '' : industry.id,
                BillingStreet: self.billingStreet(),
                BillingCity: self.billingCity(),
                BillingState: self.billingState(),
                BillingPostalCode: self.billingPostalCode(),
                BillingCountry: self.billingCountry(),
                BillingLatitude: self.billingLatitude(),
                BillingLongitude: self.billingLongitude(),
                ShippingStreet: self.shippingStreet(),
                ShippingCity: self.shippingCity(),
                ShippingState: self.shippingState(),
                ShippingPostalCode: self.shippingPostalCode(),
                ShippingCountry: self.shippingCountry(),
                ShippingLatitude: self.shippingLatitude(),
                ShippingLongitude: self.shippingLongitude(),
                Phone: self.phone(),
                MObile: self.mobile(),
                Fax: self.fax(),
                Website: self.website(),
                Email: self.email(),
                Description: self.description(),
                ImageData: self.imageData()
            };
            ajax.post('Account', form).done(function (response) {            
                if (inPicker) {
                    //  $.publish("accountAdded", [response.name]);
                    $.publish("removeCurrentPickerPane");
                } else {
                    window.location = '/account/show?id=' + response.id;
                }
            });
        };

        self.cancel = function () {
            window.location = "index";
        };
    };

    exports.exec = function (sectionId, model) {
        var viewModel = new ViewModel(model, sectionId.substring(0, 4) == "Ajax");
        ko.applyBindings(viewModel, document.getElementById(sectionId));
    };
    return exports;
});
